package controllers;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import models.User;
import play.libs.WS;
import play.mvc.Controller;
import play.server.ServletWrapper;
import utils.AppConstants;
import utils.C2DM;



public class ManageUsers extends Controller {


	/**
	 * This web service is used to register a new user if the user is already
	 * there his record is over written
	 * 
	 * @param firstName
	 * @param lastName
	 * @param mail
	 * @param phone
	 * @author Gad
	 * 
	 */
	public static void registerNewUser(String firstName, String lastName,
			String mail, String phone) {
		System.out.println(firstName + phone);
		try {
			if (!validateUserData(firstName, lastName, mail, phone)) {
				renderJSON(AppConstants.MSG_FAIL);
				return;
			}
			User s = User.findByPhone(phone);
			if (s == null) {
				s = new User(firstName, lastName, mail, phone, null);
				s.insert();
			} else {
				s.setFirstName(firstName);
				s.setLastName(lastName);
				s.setMail(mail);
				s.setDeviceRegKey(null);
				s.update();
			}
			renderJSON(s);
		} catch (Exception e) {
			renderJSON("Exception");
		}

	}

	/**
	 * updates user c2dm device key and sends notification in case of new
	 * registration
	 * 
	 * @param phone
	 *            phone number of user
	 * @param deviceRegKey
	 *            the new c2dm device key
	 */
	public static void setDeviceRegKey(String phone, String deviceRegKey) {
		User s = User.findByPhone(phone);
		try {
			boolean newReg = false;
			if (s.getDeviceRegKey() == null) {
				newReg = true;
			}
			s.setDeviceRegKey(deviceRegKey);
			s.update();
			if (newReg)
				C2DM.push(s.getPhone(), AppConstants.WELCOME_CK, AppConstants.WELCOME_MSG);
			renderJSON(AppConstants.MSG_SUCESS);
		} catch (Exception e) {
			System.out.println("Fail to reg");
			renderJSON(AppConstants.MSG_FAIL);

		}
	}

	 //debug method
	 public static void sendMsg(String phone) throws IOException{
	 System.out.println(C2DM.push(phone, AppConstants.WELCOME_CK, AppConstants.WELCOME_MSG));
	 renderText("Done");
	 }

	/**
	 * Resets the User Table in the database
	 * 
	 * @author Aya Kamel
	 */
	public static void resetTable() {
		User.resetTable();
	}

	/**
	 * Prints the User Table in the database on the browser
	 * 
	 * @author Aya Kamel
	 */
	public static void showUsers() {
		String data = User.showTable();
		renderText(data);
	}

	/**
	 * Validate user incoming data
	 * 
	 * @param fName
	 * @param lName
	 * @param mail
	 * @param mobile
	 * @return boolean
	 * @author Gad
	 */
	private static boolean validateUserData(String fName, String lName,
			String mail, String mobile) {

		Pattern namePattern = Pattern.compile("^[0-9a-zA-Z]+[a-zA-Z]*$");
		Pattern MailPattern = Pattern
				.compile("^([0-9a-zA-Z]+[-._+&amp;])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$");
		Pattern phonePattern = Pattern.compile("^[0][0][2][0][1][0-9]{9,9}$");

		if (!phonePattern.matcher(mobile).matches()
				|| !namePattern.matcher(fName).matches()
				|| !namePattern.matcher(lName).matches()
				|| !MailPattern.matcher(mail).matches())
			return false;

		return true;

	}

	public static void checkUser(String phone) {
		boolean result;
		User temp = User.findByPhone(phone);
		if (temp == null)
			result = false;
		else
			result = true;
		renderJSON(result);
	}

}
